//
//  SXPpictureView.swift
//  weibo
//
//  Created by shixinPeng on 16/3/3.
//  Copyright © 2016年 shixinPeng. All rights reserved.
//

import UIKit


/// item间隔
let ItemMargin:CGFloat = 10
/// item的宽高
let ItemWH = (kUISCREEN_W - ItemMargin * 3)/3
/// cell的标示符
let kPictureViewCellIdentifer = "kPictureViewCellIdentifer"

class SXPpictureView: UICollectionView, UICollectionViewDataSource {
    
    var pictureCollectionViewModel: SXPstatusesViewModel? {
        didSet{
            
            //获取到数据,计算collection的尺寸
            self.snp_updateConstraints { (make) -> Void in
                 make.size.equalTo(self.calculation())
            }
            //刷新界面
            reloadData()
        }
    }
    //定义一个pictureViewModel
    var pictureList: [SXPpictureViewModel]? {
        didSet{
            //获取到数据,计算collection的尺寸
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(self.calculation())
            }
            //刷新界面
            reloadData()
        }
    }
    
    

    //设置流水布局
    let flowlayout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()

    //重写初始化方法
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: flowlayout)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setUpUI() {
        
        backgroundColor = UIColor.clearColor()
        
        /*
        设置UICollectionView界面数据源方法要实现
        1.设定layout和delagate
        2.实现数据源方法
        3.显示界面--cell
        */
        self.dataSource  = self
        
        //注册cell
        self.registerClass(SXPpictureViewCell.self, forCellWithReuseIdentifier: kPictureViewCellIdentifer)
        
        //设置cell的大小
        flowlayout.itemSize = CGSizeMake(ItemWH, ItemWH)
        flowlayout.minimumInteritemSpacing = ItemMargin
        flowlayout.minimumLineSpacing = ItemMargin
        
        
        //滑动方向
        //flowlayout.scrollDirection = .Vertical
        flowlayout.scrollDirection = UICollectionViewScrollDirection.Vertical
        
        //把状态器添加到底部视图上面
        
       
        
    }
    
    private func calculation() ->CGSize {
        
        let count = pictureList?.count
       
        //printLog(count)
        if count <= 3 {
            let makeW = ItemWH * 3 + ItemMargin * 2
            let makeH = ItemWH + ItemMargin
            return CGSizeMake(makeW, makeH)
        }else if count == 4 {
            let makeW = ItemWH * 2 + ItemMargin * 1
            let makeH = (ItemWH + ItemMargin) * 2
            return CGSizeMake(makeW, makeH)
        }else{
           let row = CGFloat((count! - 1)/3 + 1)
            
           
            //printLog("row--\(row)--\(Osize)")
            
            return CGSizeMake(ItemWH * 3 + ItemMargin * 2 , row * (ItemWH + ItemMargin))
        }
        

    }
   
    
}
// MARK: - collectionView的数据源方法
extension SXPpictureView {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureList?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        //获取cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(kPictureViewCellIdentifer, forIndexPath: indexPath) as! SXPpictureViewCell
        //cell.backgroundColor = kRandomColor
        
       cell.pictureViewModel = pictureList![indexPath.item]
        
        
    
        return cell
    }
    
    
}

class SXPpictureViewCell: UICollectionViewCell {
    
    
    var pictureViewModel: SXPpictureViewModel?{
        didSet{
            
            let url = NSURL(string: pictureViewModel?.pictureModel?.thumbnail_pic ?? "")
            if let Ourl = url {
                
                iconImageView.sd_setImageWithURL(Ourl)
            }
        }
    }
    
    //第一步:重写初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
      }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    
    //第二步:添加私有方法.添加控件和添加约束
    private func setUpUI() {
        addSubview(iconImageView)
        iconImageView.frame = self.bounds
        
    }
    //第三步:懒加载
    private var iconImageView: UIImageView = {
        let img = UIImageView()
        return img
    }()
    
    

    
}
